package com.tool.emulator.apple2.wave;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * MemoryWaveOutputer.java @author tl 2011-6-26
 */
public class MemoryWaveOutputer extends WaveOutputer {
	public static final int MIN_DATA_LEN = 4;

	public MemoryWaveOutputer(long deviceFreq) throws IOException {
		super(deviceFreq, new ByteArrayOutputStream());
	}

	public synchronized byte[] finish() throws IOException {
		super.close();
		byte[] data = out().toByteArray();
		super.updateBytes(data);
		return data;
	}

	protected ByteArrayOutputStream out() {
		return (ByteArrayOutputStream) super.out;
	}

	public synchronized byte[] finishAndReset() throws IOException {
		if (super.getDataLength() < MIN_DATA_LEN) {
			out().reset();
			super.writeHeader();
			return null;
		}
		byte[] data = this.finish();
		out().reset();
		super.writeHeader();
		return data;
	}
}
